/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2022/12/19 20:04
 *  多线程的优势：提高效率
 *      但是当数据量很小的时候，由于创建线程也要花费时间，所以此时并不能提高效率
 *      线程随机性：sleep的时间是大于等于设置的时间的
 */
public class ThreadDemo16 {
    private static long count = 10_0000_0000;

    public static void main(String[] args) {
        single();
        multi();
    }

    /**
     * 单线程时间
     */
    private static void single() {
        long start  = System.currentTimeMillis();
        long a = 0;
        long b = 0;
        for (int i = 0; i < count; i++) {
            a++;
        }
        for (int i = 0; i < count; i++) {
            b++;
        }
        long end  = System.currentTimeMillis();
        System.out.println("single time : "+ (end-start)+";");
    }

    /**
     * 多线程时间
     */
    private static void multi() {
        long start  = System.currentTimeMillis();
        long a = 0;
        long b = 0;

        Thread t1 = new Thread(){
            long a = 0;
            @Override
            public void run() {
                for (int i = 0; i < count; i++) {
                    a++;
                }
            }
        };
        t1.start();
        Thread t2 = new Thread(){
            long b =0 ;
            @Override
            public void run() {
                for (int i = 0; i < count; i++) {
                    b++;
                }
            }
        };
        t2.start();
        //需要t1,t2执行完
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end  = System.currentTimeMillis();
        System.out.println("multi time : "+ (end-start)+";");
    }
}
